/**
  * <br>JAVACC DEMO 1.0<br>
  * <br>
  * @Package com.apache.dict.controller
*/
package com.apache.dict.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apache.api.api.ApiService;
import com.apache.api.api.PlatformService;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.dict.entity.DataItem;
import com.apache.dict.vo.ItemListVo;

/**
  * description: 数据字段控件：下拉列表形式
  * @author Hou DaYu 创建时间：下午03:46:47
  */
@Controller
@RequestMapping("/dict/pub")
public class DictPubAction {

	/**
	 * 数据字典条目管理的Manager
	 */
	@Autowired
	PlatformService<DataItem> dictInnerService;
	@Autowired
	ApiService dictService;

	/**
	 * 数据字典的使用：下拉列表显示；下拉列表联动
	 */
	@RequestMapping("dict-select")
	@SuppressWarnings("rawtypes")
	@ResponseBody
	public String doList(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = false) String cateEname) {
		ParamsVo vo = new ParamsVo();
		vo.setMethodKey("getList");
		Map para = request.getParameterMap();
		//如果传递了fatherValue参数，并且为空，则不取数据
		//防止诸如城市列表，都列出来
		String fatherValue = request.getParameter("fatherValue");
		request.setAttribute("cateEname", cateEname);
		if (para.containsKey("fatherValue") && Validator.isNull(fatherValue)) {
			vo.setParams("ParameterMap", true);
		}
		vo.setParams("cateEname", cateEname);
		vo.setParams("fatherValue", fatherValue);
		request.setAttribute("fatherValue", fatherValue);

		ItemListVo item = new ItemListVo();
		item.setCateEname(cateEname);
		item.setFatherValue(fatherValue);
		vo.setObj(item);
		return dictInnerService.doService(vo);
	}

	/**
	 * 数据字典的使用：下拉列表显示；下拉列表联动
	 */
	@RequestMapping("dict-select.action")
	@ResponseBody
	public JSONArray doListJsoN(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = false) String cateEname) {
		String result = doList(request, cateEname);
		JSONArray array = JSONArray.fromObject(result);
		return array;
	}

	@RequestMapping("getFatherItem.action")
	@ResponseBody
	public JSONArray getFatherItemJson(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = false) String cateEname) {
		String result = getFatherItem(request, cateEname);
		JSONArray array = JSONArray.fromObject(result);
		return array;
	}

	@RequestMapping("getFatherItem")
	@SuppressWarnings("rawtypes")
	@ResponseBody
	public String getFatherItem(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = false) String cateEname) {
		ParamsVo vo = new ParamsVo();
		vo.setMethodKey("getList");
		Map para = request.getParameterMap();
		//如果传递了fatherValue参数，并且为空，则不取数据
		//防止诸如城市列表，都列出来
		String fatherId = request.getParameter("fatherId");
		request.setAttribute("cateEname", cateEname);
		if (para.containsKey("fatherValue") && Validator.isNull(fatherId)) {
			vo.setParams("ParameterMap", true);
		}
		vo.setParams("cateEname", cateEname);
		vo.setParams("fatherId", fatherId);

		request.setAttribute("fatherId", fatherId);
		ItemListVo item = new ItemListVo();
		item.setCateEname(cateEname);
		item.setFatherId(fatherId);
		vo.setObj(item);
		return dictInnerService.doService(vo);
	}

	/**
	 * 数据字段控件：树形列表形式
	 */
	@RequestMapping("dict-tree.action")
	@ResponseBody
	public JSONArray doTreeJson(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = false) String cateEname) {
		String result = doTree(request, cateEname);
		JSONArray array = JSONArray.fromObject(result);
		return array;
	}

	/**
	 * 数据字段控件：树形列表形式
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("dict-tree")
	@ResponseBody
	public String doTree(HttpServletRequest request,
			@RequestParam(value = "cateEname", required = true) String cateEname) {
		//String cateEname = request.getParameter("cateEname");//科目标识
		ParamsVo vo = new ParamsVo();
		vo.setMethodKey("getTree");
		String id = request.getParameter("id");//条目名称
		String value = request.getParameter("value");//条目标识
		if (Validator.isNull(id)) {
			id = "0";
		}
		vo.setParams("id", id);
		vo.setParams("value", value);
		vo.setParams("cateEname", cateEname);
		return dictInnerService.doService(vo);
	}

	/**
	 * 下载数据字典
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping("dict-download")
	@ResponseBody
	public String dictDownLoad(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "cateEname", required = true) String cateEname) {
		//String cateEname = request.getParameter("cateEname");//科目标识
		String fatherId = request.getParameter("fatherId");//条目名称
		String fatherValue = request.getParameter("fatherValue");//条目标识
		String fileName = Validator.getDefaultStr(cateEname, String.valueOf(new Date().getTime()));
		fileName += ".json";
		response.setContentType("application/x-download;charset=UTF-8");
		response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
		ParamsVo vo = new ParamsVo();
		vo.setMethodKey("dictDownLoad");
		vo.setParams("fatherId", fatherId);
		vo.setParams("fatherValue", fatherValue);
		vo.setParams("cateEname", cateEname);

		//如果传递了fatherValue参数，并且为空，则不取数据
		//防止诸如城市列表，都列出来
		Map para = request.getParameterMap();
		if (para.containsKey("fatherValue") && Validator.isNull(fatherValue)) {
			vo.setParams("ParameterMap", true);
		}
		return dictInnerService.doService(vo);
	}

	/**
	 * 数据字典的使用：下拉列表显示；下拉列表联动
	 */
	@RequestMapping("dict!city.action")
	@ResponseBody
	public JSONObject doCityJsoN(HttpServletRequest request, String cateEname, String fatherValue) {
		ParamsVo vo = new ParamsVo();
		vo.setMethodKey("cityList");
		vo.setParams("cateEname", cateEname);
		vo.setParams("fatherValue", fatherValue);
		ResultEntity entity = dictService.doService(vo);
		if ("true".equals(entity.getResult())) {
			JSONObject array = JSONObject.fromObject(entity.getEntity());
			return array;
		} else {
			JSONObject array = JSONObject.fromObject("{}");
			return array;
		}
	}
}
